package com.customization.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 接口结果类
 * Created by YeShengtao on 2020/9/29 00:06
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Accessors(chain = true)
public class Result {
    private String result;
    private String msg;
    private Object data;

    public String getResult() {
        return result;
    }

    public Result setResult(String result) {
        this.result = result;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getData() {
        return data;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    private static final String OK_CODE = "0";
    private static final String OK_MSG = "操作成功";
    private static final String ERR_CODE = "10005";
    private static final String ERR_MSG = "操作失败";
    private static final String TOKEN_ERR_CODE = "10001";
    private static final String TOKEN_ERR_MSG = "标识不存在或未启用";
    private static final String PARAM_ERR_CODE = "10002";
    private static final String PARAM_ERR_MSG = "参数不正确";
    private static final String EXCEPTION_ERR_CODE = "10003";
    private static final String EXCEPTION_ERR_MSG = "发生异常";

    public static Result create(String result, String msg) {
        return new Result()
                .setResult(result)
                .setMsg(msg);
    }

    public static Result create(String result, String msg, Object data) {
        return new Result()
                .setResult(result)
                .setMsg(msg)
                .setData(data);
    }

    public static Result fail() {
        return create(ERR_CODE, ERR_MSG);
    }

    public static Result fail(String msg) {
        return create(ERR_CODE, msg);
    }

    public static Result ok() {
        return create(OK_CODE, OK_MSG);
    }

    public static Result ok(Object data) {
        return create(OK_CODE, OK_MSG, data);
    }

    public static Result exception() {
        return create(EXCEPTION_ERR_CODE, EXCEPTION_ERR_MSG);
    }
    public static Result exception(Object data) {
        return create(EXCEPTION_ERR_CODE, EXCEPTION_ERR_MSG, data);
    }


    public static Result tokenErr() {
        return create(TOKEN_ERR_CODE, TOKEN_ERR_MSG);
    }

    public static Result paramErr() {
        return create(PARAM_ERR_CODE, PARAM_ERR_MSG);
    }
}
